%% this function breaks the pairs and reshuffles them
function [corr, p, corr_iter] = corr_resampling(x, y, N)
% N = number of times to resample data
ns = length(x);

corr = corrcoef(x,y);%correlation coeficient for normally distributed samples
corr = corr(2,1);

corr_iter = zeros(N,1);%vector of 5000 values of possible correlation coef with a random assignment between x and y

for i=1:N
    shuf_order = randperm(ns);%vector with randomly distributed number from 1 to ns
    
    new_y = y(shuf_order);%% (shift the order of elements in y by using the order in vector shuf_order)
    r_aux = corrcoef(x, new_y);
    corr_iter(i) = r_aux(1,2);
end

if corr > 0
   p =  sum(corr_iter>=corr)/N*2; %probability that the new correlation coeficient that is randomly generated is bigger than my real coeficient (N*2 beacuse 2 tail)
else
   p =  sum(corr_iter<=corr)/N*2; 
end
